package repository

import (
	"context"
	article2 "jk-time/webook/internal/dao/article"
	"jk-time/webook/internal/domain"
)

// 演示在service层面上分流
type ArticleReaderRepository interface {
	Save(ctx context.Context, art domain.Article) error
}

// CachedArticleReaderRepository 按照道理，这里也是可以搞缓存的
type CachedArticleReaderRepository struct {
	dao article2.ArticleReaderDAO
}

func NewCachedArticleReaderRepository(dao article2.ArticleReaderDAO) ArticleReaderRepository {
	return &CachedArticleReaderRepository{
		dao: dao,
	}
}
func (c *CachedArticleReaderRepository) Save(ctx context.Context, art domain.Article) error {
	return c.dao.Upsert(ctx, article2.PublishedArticle(c.toEntity(art)))
}
func (c *CachedArticleReaderRepository) toEntity(art domain.Article) article2.Article {
	return article2.Article{
		Id:       art.Id,
		Title:    art.Title,
		Content:  art.Content,
		AuthorId: art.Author.Id,
		Status:   art.Status.ToUint8(),
	}
}
